# vim:ts=4:sw=4:noet:
"""
VM remote object proxies 

When interacting with the VM via L{Remote}, SimpleObjs are proxies representing
objects resident in the remote VMs memory. 

Since the Foxtalk VM uses an optimised representation for Integers -8191 to 8192,
there's also a SimpleSmallInt proxy.
"""

class SimpleObj:
	""" Remote Object Proxy """
	def __init__(self, remote, ptr, cls, size, bin, data, trunc):
		self.remote = remote
		self.ptr = ptr
		self.cls = cls
		self.size = size
		self.bin = bin
		self.data = data
		self.trunc = trunc

	def __repr__(self):
		return "<obj@%d: %d %s>" % (self.ptr, self.size, self.data)

	def setData(self, idx, val):
		self.data[idx] = val
		self.remote.setObjectData(self.ptr, idx, val)

	def asString(self):
		s = ""
		for c in self.data:
			s += chr(c)
		return s

	def asPrettyString(self, remote):
		acls = remote.getObject(self.cls)
		aclsName = remote.getObject(acls.data[0]).asString()
		return "<%s@%d: %d %s>" % (aclsName, self.ptr, self.size, self.data)

class SimpleSmallInt:
	""" Smallint Object Proxy """
	def __init__(self, val):
		self.val = val
	
	def asPrettyString(self, remote):
		return "<SmallInt: %d>" % self.val

	def asString(self):
		return str(self.val)
